package com.emm.yixun.soap.client;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.soap.util.httpclient.RequestUtil;
import com.emm.yixun.soap.voip.ExternalServiceStub;
import com.emm.yixun.soap.voip.ExternalServiceStub.Dial;
import com.emm.yixun.soap.voip.ExternalServiceStub.DialE;
import com.emm.yixun.soap.voip.ExternalServiceStub.DialResponseE;
import com.emm.yixun.soap.voip.ExternalServiceStub.DialTelephoneInput;

public class NetworkTelephoneClient {
	private static String token="5d61a0a1-036f-4c67-950c-7a386fecbef2";
	private static Long accountId=Long.valueOf("500059900");
	private static String callCenterType="B";
	private static String shopCode="59900101";
	/*
	 * 用途：调用电话平台(房信通)回拨接口
	 * 参数:caller:主叫号码  called:被叫号码 
	 * 返回:JSONObject 当errMsg不为空时，视为调用失败；当为空时视为调用成功，会返回唯一码
    */	
	public static JSONObject call(String caller,String called){
		JSONObject object=new JSONObject();
		if(null==caller || caller.equals("") || null==called || called.equals("")){
			object.put("errMsg", "主叫或被叫号码不能为空");
			object.put("uniqueId", "");
			return object;
		}
		try {
			DialTelephoneInput dti=new DialTelephoneInput();
			dti.setToken(token);
			dti.setAccountId(accountId);
			dti.setCallCenterType(callCenterType);
			dti.setShopCode(shopCode);
			//被叫
			dti.setCalled(called);
			//主叫
			dti.setCaller(caller);
			
			Dial dial=new Dial();
			dial.setArg0(dti);
			
			DialE dialE=new DialE();
			dialE.setDial(dial);
			ExternalServiceStub ess=new ExternalServiceStub();
			DialResponseE dre= ess.dial(dialE);
			String errMsg=dre.getDialResponse().get_return().getErrorMessage();
			String uniqueId=dre.getDialResponse().get_return().getUniqueId();
			String call_session_uuid=dre.getDialResponse().get_return().getCall_session_uuid();
			if(null==errMsg)errMsg="";
			if(null==uniqueId)uniqueId="";
			if(null==call_session_uuid)call_session_uuid="";
			object.put("errMsg", errMsg);
			object.put("uniqueId", uniqueId);
		} catch (Exception e) {
			object.put("errMsg", "接口调用失败");
			object.put("uniqueId", "");
		}
		return object;
	}
	
	/*
	 * 用途：获取话单(房信通)
	 * 参数:uniqueIdString:唯一标示，以逗号隔开
	 * 返回:JSONObject,格式：{"status":1,"message":"success","dataList":[]} 
    */
	
	public static JSONObject getCallRecord(String uniqueIdString){
		String postUrl="http://c.51fxt.com/services/api/getCallDetailRecordByCallBack/http.do";
		JSONObject object=null;
		Map<String, String> paramMap=new HashMap<String, String>();
		paramMap.put("token", token);
		paramMap.put("uniqueIdString", uniqueIdString);
		try {
			String result=RequestUtil.buildRequest(postUrl, paramMap, "UTF-8");
			object=JSON.parseObject(result);
		} catch (Exception e) {
			object=new JSONObject();
			object.put("message", "error");
			object.put("status", "0");
			object.put("dataList", new JSONArray());
		}
		return object;
	}
	
	public static void main(String[] args) {
		JSONObject callResult=call("15021179201","18670611724");
		System.out.println("callResult:"+callResult.toJSONString());
		/*JSONObject callRecordResult=getCallRecord("161101164014105100010226001e4136,161101165255927400010185001e4ee1");
		System.out.println("callRecordResult:"+callRecordResult.toJSONString());*/
	}
	
}
